#include <stdlib.h>

#include "battery.h"
#include "pico.h"

typedef struct pico {
	uv_loop_t *loop;
	struct battery *battery;
} pico_t;

int32_t pico_run(uv_loop_t *loop)
{
	pico_t *pico;
	int32_t ret;

	pico = calloc(1, sizeof(pico_t));
	if (!pico)
		goto pico_calloc_fail;

	pico->loop = loop;

	pico->battery = battery_create(loop, BUS_I2C, 2);
	if (!pico->battery)
		goto battery_create_fail;

	//start battery monitor timer
	battery_start_monitor(pico->battery);

	if (uv_run(loop, UV_RUN_DEFAULT))
		abort();

	battery_destroy(pico->battery);
	uv_loop_delete(loop);

	return 0;

battery_create_fail:
	free(pico);
pico_calloc_fail:
	return -1;
}
